<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/images/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="quitLogin()">退出</el-button>
    </el-header>
    <!-- 主页区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? 64 + 'px' : 200 + 'px'">
        <div class="toggle-button" @click="togglebtn">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#379AFF"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          router
          :default-active="savePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]" id="iconfl"></i>
              <!-- 文本区 -->
              <span v-text="item.authName"></span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveIndex('/' + subItem.path)"
            >
              <!-- 二级菜单模板区 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本区 -->
                <span v-text="subItem.authName"></span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menulist: [],
      iconsObj: {
        '125': 'el-icon-s-custom',
        '103': 'el-icon-box',
        '101': 'el-icon-suitcase',
        '102': 'el-icon-s-order',
        '145': 'el-icon-s-marketing'
      },
      // 是否折叠
      iscollapse: false,
      // 被激活的链接地址
      savePath: ''
    }
  },
  // 钩子函数Domx渲染之后调用
  created() {
    this.getMenuList()
    this.savePath = window.sessionStorage.getItem('savePath')
  },
  methods: {
    quitLogin() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)

      // 如果请求失败
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    togglebtn() {
      this.iscollapse = !this.iscollapse
    },
    // 保存链接的激活状态
    saveIndex(savei) {
      window.sessionStorage.setItem('savePath', savei)
      this.savePath = savei
    }
  }
}
</script>
<style scoped lang="less">
.home_container {
  width: 100%;
  height: 100%;
  .el-header {
    background-color: #363c42;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
      span {
        margin-left: 20px;
      }
    }
  }
  .el-aside {
    background-color: #313743;
    .toggle-button {
      background-color: #495065;
      text-align: center;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
    #iconfl {
      padding-right: 10px;
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-main {
    background-color: #e9edf1;
  }
}
</style>
